/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.william;

import l1j.server.server.datatables.ItemTable;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.templates.L1Item;
import l1j.server.server.templates.L1Npc;

import l1j.william.ItemSummon;

public class L1WilliamItemSummon {

	public static int checkItemId(final int itemId) {// TODO 判斷有沒有這個編號的道具
		final L1WilliamItemSummon Item_Summon = ItemSummon.getInstance()
				.getTemplate(itemId);

		if (Item_Summon == null) {
			return 0;
		}

		final int item_id = Item_Summon.getItemId();
		return item_id;
	}

	public static void getItemSummon(final L1PcInstance pc,
			final L1ItemInstance l1iteminstance, final int itemId) {
		final L1WilliamItemSummon Item_Summon = ItemSummon.getInstance()
				.getTemplate(itemId);

		boolean isSummon = false;
		boolean isConsumeItem = false;

		if (Item_Summon == null) {
			return;
		}

		if (Item_Summon.getCheckLevel() != 0) {// TODO 等級判斷
			if (pc.getLevel() < Item_Summon.getCheckLevel()) {// TODO 等級不符
				pc.sendPackets(new S_ServerMessage(318, ""
						+ Item_Summon.getCheckLevel()));// TODO 等級%0以上才可使用此道具。
				return;
			}
		}

		if (Item_Summon.getCheckClass() != 0) {// TODO 職業判斷
			byte class_id = (byte) 0;
			String msg = "";

			if (pc.isCrown()) {// TODO 王族
				class_id = 1;
			} else if (pc.isKnight()) {// TODO 騎士
				class_id = 2;
			} else if (pc.isWizard()) {// TODO 法師
				class_id = 3;
			} else if (pc.isElf()) {// TODO 妖精
				class_id = 4;
			} else if (pc.isDarkelf()) {// TODO 黑妖
				class_id = 5;
			} else if (pc.isDragonKnight()) {// TODO 龍騎士
				class_id = 6;
			} else if (pc.isIllusionist()) {// TODO 幻術師
				class_id = 7;
			}

			switch (Item_Summon.getCheckClass()) {
			case 1:
				msg = "王族";
				break;
			case 2:
				msg = "騎士";
				break;
			case 3:
				msg = "法師";
				break;
			case 4:
				msg = "妖精";
				break;
			case 5:
				msg = "黑妖";
				break;
			case 6:
				msg = "龍騎士";
				break;
			case 7:
				msg = "幻術師";
				break;
			}

			if (Item_Summon.getCheckClass() != class_id) {// TODO 職業不符
				pc.sendPackets(new S_ServerMessage(166, "職業必須是" + msg
						+ "才能使用此道具"));
				return;
			}
		}

		if (pc.getCha() < Item_Summon.getSummonCost()) {// TODO 魅力不足
			pc.sendPackets(new S_ServerMessage(166, "魅力最低需要" + " ("
					+ Item_Summon.getSummonCost() + ")"));
			return;
		}

		if (Item_Summon.getCheckItem() != 0) {// TODO 攜帶物品判斷
			if (!pc.getInventory().checkItem(Item_Summon.getCheckItem())) {
				final L1Item temp = ItemTable.getInstance().getTemplate(
						Item_Summon.getCheckItem());
				pc.sendPackets(new S_ServerMessage(166, "必須有(" + temp.getName()
						+ ") 才能使用此道具"));
				return;
			}
		}

		if (Item_Summon.getHpConsume() != 0 || Item_Summon.getMpConsume() != 0) {// TODO
																					// 體力&&
																					// 魔力判斷
			if (pc.getCurrentHp() < Item_Summon.getHpConsume()) {
				pc.sendPackets(new S_ServerMessage(166, "必須有("
						+ Item_Summon.getHpConsume() + ") HP 才能使用此道具"));
				return;
			}
			if (pc.getCurrentMp() < Item_Summon.getMpConsume()) {
				pc.sendPackets(new S_ServerMessage(166, "必須有("
						+ Item_Summon.getMpConsume() + ") MP 才能使用此道具"));
				return;
			}
		}

		if (Item_Summon.getMaterial() != 0
				&& Item_Summon.getMaterialCount() != 0) {// TODO 媒介判斷
			if (!pc.getInventory().checkItem(Item_Summon.getMaterial(),
					Item_Summon.getMaterialCount())) {
				final L1Item temp = ItemTable.getInstance().getTemplate(
						Item_Summon.getMaterial());
				pc.sendPackets(new S_ServerMessage(337, temp.getName()
						+ "("
						+ (Item_Summon.getMaterialCount() - pc.getInventory()
								.countItems(temp.getItemId())) + ")"));
				return;
			} else {
				isConsumeItem = true;
			}
		}

		if (pc.getMap().isRecallPets()) {
			int summonCost = Item_Summon.getSummonCost();// TODO 召喚所需魅力
			if (summonCost < 6) {
				summonCost = 6;
			}

			int petcost = 0;
			for (final Object pet : pc.getPetList().values().toArray()) {
				petcost += ((L1NpcInstance) pet).getPetcost();// TODO 取得已使用的魅力
			}

			int charisma = pc.getCha();

			if (pc.isCrown()) {// TODO 王族
				charisma += 6;
			} else if (pc.isKnight()) {// TODO 騎士
				charisma += 12;
			} else if (pc.isWizard()) {// TODO 法師
				charisma += 6;
			} else if (pc.isElf()) {// TODO 妖精
				charisma += 12;
			} else if (pc.isDarkelf()) {// TODO 黑妖
				charisma += 6;
			} else if (pc.isDragonKnight()) {// TODO 龍騎士
				charisma += 6;
			} else if (pc.isIllusionist()) {// TODO 幻術師
				charisma += 6;
			}

			charisma -= petcost;
			int summonCount = charisma / summonCost;
			if (summonCount == 0) {
				if (petcost > 0) {// TODO 已有召喚怪
					pc.sendPackets(new S_ServerMessage(489));// TODO
																// 你無法一次控制那麼多寵物。
					return;
				} else {
					pc.sendPackets(new S_ServerMessage(166, "魅力最低需要" + " ("
							+ summonCost + ")"));
				}
			}

			if (Item_Summon.getOnlyOne() != 0) {// TODO 召喚隻數限制為 1
				for (final Object petNpc : pc.getPetList().values().toArray()) {
					if (petNpc instanceof L1SummonInstance) {
						final L1SummonInstance _summon = (L1SummonInstance) petNpc;
						if (_summon != null) {// TODO 已有其他召喚怪
							pc.sendPackets(new S_ServerMessage(489));// TODO
																		// 你無法一次控制那麼多寵物。
							return;
						}
					} else if (petNpc instanceof L1PetInstance) {
						final L1PetInstance _pet = (L1PetInstance) petNpc;
						if (_pet != null) {// TODO 已有其他寵物
							pc.sendPackets(new S_ServerMessage(489));// TODO
																		// 你無法一次控制那麼多寵物。
							return;
						}
					}
				}
				summonCount = 1;
			}

			final L1Npc npcTemp = NpcTable.getInstance().getTemplate(
					Item_Summon.getSummonId());
			for (int i = 0; i < summonCount; i++) {
				final L1SummonInstance summon = new L1SummonInstance(npcTemp,
						pc);
				if (Item_Summon.getOnlyOne() != 0) {
					summon.setPetcost(pc.getCha() + 7);
				} else {
					summon.setPetcost(summonCost);
				}
				isSummon = true;
			}
		} else {
			pc.sendPackets(new S_ServerMessage(353));// TODO 在這附近無法召喚怪物。
		}

		if (isSummon) {// TODO 召喚成功
			if (isConsumeItem) {// TODO 刪除召喚媒介
				pc.getInventory().consumeItem(Item_Summon.getMaterial(),
						Item_Summon.getMaterialCount());
			}
			if (Item_Summon.getRemoveItem() != 0) {// TODO 刪除召喚道具
				pc.getInventory().removeItem(l1iteminstance, 1);
			}
			pc.setCurrentHp(pc.getCurrentHp() - Item_Summon.getHpConsume());// TODO
																			// 扣血
			pc.setCurrentMp(pc.getCurrentMp() - Item_Summon.getMpConsume());// TODO
																			// 扣魔
		}
	}

	private final int _item_id;

	private final int _checkLevel;

	private final int _checkClass;

	private final int _checkItem;

	private final int _hpConsume;

	private final int _mpConsume;

	private final int _material;

	private final int _material_count;

	private final int _summon_id;

	private final int _summonCost;

	private final int _onlyOne;

	private final int _removeItem;

	public L1WilliamItemSummon(final int item_id, final int checkLevel,
			final int checkClass, final int checkItem, final int hpConsume,
			final int mpConsume, final int material, final int material_count,
			final int summon_id, final int summonCost, final int onlyOne,
			final int removeItem) {

		this._item_id = item_id;
		this._checkLevel = checkLevel;
		this._checkClass = checkClass;
		this._checkItem = checkItem;
		this._hpConsume = hpConsume;
		this._mpConsume = mpConsume;
		this._material = material;
		this._material_count = material_count;
		this._summon_id = summon_id;
		this._summonCost = summonCost;
		this._onlyOne = onlyOne;
		this._removeItem = removeItem;
	}

	public int getCheckClass() {
		return this._checkClass;
	}

	public int getCheckItem() {
		return this._checkItem;
	}

	public int getCheckLevel() {
		return this._checkLevel;
	}

	public int getHpConsume() {
		return this._hpConsume;
	}

	public int getItemId() {
		return this._item_id;
	}

	public int getMaterial() {
		return this._material;
	}

	public int getMaterialCount() {
		return this._material_count;
	}

	public int getMpConsume() {
		return this._mpConsume;
	}

	public int getOnlyOne() {
		return this._onlyOne;
	}

	public int getRemoveItem() {
		return this._removeItem;
	}

	public int getSummonCost() {
		return this._summonCost;
	}

	public int getSummonId() {
		return this._summon_id;
	}
}